package com.easy.component.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * 描述: {描述}
 * 作者:
 * 创建日期: 2017/11/2 16:56
 * 修改日期: 2017/11/2
 * 版本: {版本}
 */
public class ImageLoader {
    private static final ImageLoader ourInstance = new ImageLoader();

    public static ImageLoader getInstance() {
        return ourInstance;
    }

    private ImageLoader() {
    }

    /**
     * @param mContext
     * @param view
     * @param imgUrl
     * @param defaultUrl
     */
    public void display(Context mContext, ImageView view, String imgUrl, int defaultUrl) {
        if (null == mContext) {
            return;
        }
        Glide.with((mContext instanceof Activity) ? (Activity) mContext : mContext).load(imgUrl)
                .asBitmap()
                .placeholder(defaultUrl)
                .error(defaultUrl)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    /**
     * @param mContext
     * @param view
     * @param imgUrl
     * @param defaultUrl
     */
    public void displayGlideRadius(Context mContext, ImageView view, String imgUrl, int defaultUrl) {
        displayGlideRadius(mContext, view, imgUrl, defaultUrl, 2);
    }

    /**
     * @param mContext
     * @param view
     * @param imgUrl
     * @param defaultUrl
     */
    public void displayGlideRadius(Context mContext, ImageView view, String imgUrl, int defaultUrl, int radius) {
        if (null == mContext) {
            return;
        }
        Glide.with((mContext instanceof Activity) ? (Activity) mContext : mContext).load(imgUrl)
                .asBitmap()
                .placeholder(defaultUrl)
                .error(defaultUrl)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRadiusTransform(mContext, radius))
                .into(view);
    }

    /**
     * @param mContext
     * @param view
     * @param imgUrl
     * @param defaultUrl
     */
    public void displayGlideRadius(Context mContext, ImageView view, String imgUrl, int defaultUrl, int radius, boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
        if (null == mContext) {
            return;
        }
        RoundedCornersTransform transform = new RoundedCornersTransform(mContext, radius, 0, RoundedCornersTransform.CornerType.OTHER_TOP_LEFT);
        Glide.with((mContext instanceof Activity) ? (Activity) mContext : mContext).load(imgUrl)
                .asBitmap()
                .placeholder(defaultUrl)
                .error(defaultUrl)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(transform)
                .into(view);
    }

    /**
     * @param mContext
     * @param view
     * @param imgUrl
     * @param defaultUrl
     */
    public void displayBlurTransformation(Context mContext, ImageView view, String imgUrl, int defaultUrl) {
        if (null == mContext) {
            return;
        }
        Glide.with((mContext instanceof Activity) ? (Activity) mContext : mContext).load(imgUrl)
                .bitmapTransform(new BlurTransformation(mContext, 25))
                .placeholder(defaultUrl)
                .error(defaultUrl)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    //加载网络图片显示为圆形图片
    public void displayCricleImage(Context mContext, ImageView imageView, String url, int defaultImge) {
        if (null == mContext) {
            return;
        }
        Glide.with((mContext instanceof Activity) ? (Activity) mContext : mContext)
                .load(url)
                .transform(new GlideCircleTransform(mContext))
                .crossFade()
                .placeholder(defaultImge)
                .into(imageView);
    }


}
